﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.IO;
using System.Drawing;
using System.ComponentModel;
using System.Collections;
using System.Text.RegularExpressions;
using DTO;
using BUS;

namespace QuanLyNhaSach
{
    static class Global
    {
        public static int UserID = -1;
        public static int GroupID = -1;
        public static string DateTimeFormat = "dd/MM/yyyy";
        public static string NumberFormat = "N0";
        public static string CurrencyFormat = "0,0";

        public static void OpenTabPages(int key, DevComponents.DotNetBar.Office2007RibbonForm frm)
        {
            frmMain frmM = (frmMain)Application.OpenForms["frmMain"];
            frmM.OpenTab(key,frm);
        }

        public static void GenerateNumber(DevComponents.DotNetBar.Controls.DataGridViewX grid, int col)
        {
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Cells[col].Value = i + 1;
            }
        }

        public static void GenerateEditColumn(DevComponents.DotNetBar.Controls.DataGridViewX grid, int col)
        {
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Cells[col].Value = Properties.Resources.edit_16;
                grid.Rows[i].Cells[col].ToolTipText = "Sửa";
            }
        }

        public static void GenerateDeleteColumn(DevComponents.DotNetBar.Controls.DataGridViewX grid, int col)
        {
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Cells[col].Value = Properties.Resources.deletered;
                grid.Rows[i].Cells[col].ToolTipText = "Xóa";
            }
        }

        public static void SetMessage(ToolStripStatusLabel label, string message, bool isSuccess)
        {
            if (isSuccess)
            {
                label.ForeColor = Color.Green;
                label.Text = message;
            }
            else
            {
                label.ForeColor = Color.Red;
                label.Text = message;
            }
        }

        public static DialogResult ShowMessageBoxDelete(string mess)
        {
            return DevComponents.DotNetBar.MessageBoxEx.Show( mess, "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
        }

        public static bool TestEmailRegex(string emailAddress)
        {
            // string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            // Regex reLenient = new Regex(patternLenient);
            //  bool isLenientMatch = reLenient.IsMatch(emailAddress
            // return isLenientMatch;

            string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"

                  + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"

                  + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

                  + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"

                  + @"[a-zA-Z]{2,}))$";

            Regex reStrict = new Regex(patternStrict);
            bool isStrictMatch = reStrict.IsMatch(emailAddress);
            return isStrictMatch;
        }

        public static bool NumberOnlyRegex(string number)
        {
            string patternStrict = @"[0-9]{6,15}";

            Regex reStrict = new Regex(patternStrict);
            bool isStrictMatch = reStrict.IsMatch(number);
            return isStrictMatch;
        }

        public static int intParse(object obj)
        {
            int num = 0;
            if (obj == null)
            {
                return 0;
            }

            if (int.TryParse(obj.ToString(), out num))
            {
                return num;
            }
            else
            {
                return 0;
            }
        }

        public static float floatParse(object obj)
        {
            float f = 0;
            if (obj == null)
            {
                return 0;
            }

            if (float.TryParse(obj.ToString(), out f))
            {
                return f;
            }
            else
            {
                return 0;
            }
        }

        public static string stringParse(object obj)
        {
            string str = string.Empty;
            if (obj == null)
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }

        public static bool ValidateCustomerCode(string code)
        {
            DocSequenceBUS docSeqBUS = new DocSequenceBUS();
            DocSequenceDTO docSeqDto = docSeqBUS.GetDocSequenceByName(DocSequence.Customer.ToString());
            string format = @"[" + docSeqDto.DocSequenceCode + "]{1}[0-9]{" + docSeqDto.NumLength.ToString() + "}$";
            Regex reg = new Regex(format);
            return reg.IsMatch(code);
        }
    }
}
